// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Devel.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework
{
    #region Usings

    using System;
    using System.ComponentModel;
    using System.Threading;

    #endregion

    /// <summary>
    /// The devel.
    /// </summary>
    public class Devel
    {
        #region Delegates

        /// <summary>
        /// The empty delegate.
        /// </summary>
        public delegate void EmptyDelegate();

        /// <summary>
        /// The empty delegate.
        /// </summary>
        /// <typeparam name="T">
        /// </typeparam>
        public delegate T EmptyDelegate<T>();

        #endregion

        #region Public Methods

        /// <summary>
        /// The async execute.
        /// </summary>
        /// <param name="captureException">
        /// The captureException.
        /// </param>
        /// <param name="d">
        /// The d.
        /// </param>
        public static void AsyncExecute(bool captureException, EmptyDelegate d)
        {
            if (!captureException)
            {
                d.BeginInvoke(null, null);
            }
            else
            {
                try
                {
                    d.BeginInvoke(null, null);
                }
                catch (Win32Exception e)
                {
                    var breakpoint = e.ToString();
                }
                catch (Exception e)
                {
                    var breakpoint = e.ToString();
                }
            }
        }

        /// <summary>
        /// The breakpoint.
        /// </summary>
        /// <param name="o">
        /// The o.
        /// </param>
        public static void Breakpoint(object o)
        {
#if DEBUG
            if (true)
            {
                var breakpoint = o; // <- put a red button there ;-) 
            }

#endif
        }

        /// <summary>
        /// The executed from.
        /// </summary>
        /// <param name="message">
        /// The message.
        /// </param>
        public static void ExecutedFrom(string message)
        {
            Stdout.WriteLine(
                "AppDomain {0}\"{1}\" Thread \"{2}:{3}\" {4}", 
                AppDomain.CurrentDomain.Id, 
                AppDomain.CurrentDomain.FriendlyName, 
                Thread.CurrentThread.ManagedThreadId, 
                Thread.CurrentThread.Name, 
                message);
        }

        /// <summary>
        /// The no throw.
        /// </summary>
        /// <param name="d">
        /// The d.
        /// </param>
        public static void NoThrow(EmptyDelegate d)
        {
            try
            {
                d();
            }
            catch (Win32Exception e)
            {
                var breakpoint = e.ToString();
            }
            catch (Exception e)
            {
                var breakpoint = e.ToString();
            }
        }

        /// <summary>
        /// The no throw.
        /// </summary>
        /// <param name="d">
        /// The d.
        /// </param>
        /// <typeparam name="T">
        /// </typeparam>
        /// <returns>
        /// </returns>
        public static T NoThrow<T>(EmptyDelegate<T> d)
        {
            try
            {
                return d();
            }
            catch (Exception e)
            {
                var breakpoint = e.ToString();
            }

            return default(T);
        }

        /// <summary>
        /// The not yet implemented.
        /// </summary>
        public static void NotYetImplemented()
        {
            Stderr.WriteLine("Not Yet Implemented, below is the stack trace");
            Stderr.WriteLine(Environment.StackTrace);
        }

        /// <summary>
        /// The should not occur.
        /// </summary>
        /// <param name="caller">
        /// The caller.
        /// </param>
        /// <param name="s">
        /// The s.
        /// </param>
        public static void ShouldNotOccur(object caller, string s)
        {
            Stderr.WriteLine("ShouldNotOccur: " + s);
        }

        /// <summary>
        /// The should not occur.
        /// </summary>
        /// <param name="s">
        /// The s.
        /// </param>
        public static void ShouldNotOccur(string s)
        {
            Stderr.WriteLine("ShouldNotOccur: " + s);
        }

        #endregion
    }
}